//产品视图
var exports = {};

var panel = $(".chooseProduct");

var productListData = null;
var _currPageIndex = 1;
var _pageMax = 1;
var _limit = 20;
var _moudleCategoryDatas = null;
var _topClassValue = "";
var _productMap = [];

var _searchKey = '';


function getMoudleCategoryDatas(callBack){
	var url = config_gl.apiPath+"moduleCategory/levelTwo";
	$.ajax(url, {
		type: 'post', //HTTP请求类型
		contentType: "application/json",
		success: function(data) {
			callBack(data);
		},
		error: function(xhr, type, errorThrown) {
			layer.alert("获取图库分类未知错误");
		}
	});
}

exports.init = function() {
	getMoudleCategoryDatas(function(data){
		_moudleCategoryDatas = data.data;
		updateTopClassView();
	});
	
	renderImgList();
}

$(".chooseProduct").find(".searchBtn").on("click", doSearchBtn);
function doSearchBtn(){
	var key = $(".chooseProduct .searchText").val();
	_searchKey = $.trim(key);
	renderImgList();
}

function updateTopClassView(){
	var uldom = panel.find(".product-cate-type");
	uldom.empty();
	var list = _moudleCategoryDatas;
	uldom.append('<option value="">全部</option>');
	for(var i=0; i<list.length; i++){
		var d = list[i];
		uldom.append('<option value="'+d.id+'">'+d.name+'</option>');
	}
	
	layui.form.on('select(productType)', function(data){
		_topClassValue = data.value;
		
		renderImgList();
	});
	
	layui.form.render('select');
}

function getProductDatas(callBack){
	var fdata = {
		"limit": _limit,
		"page": _currPageIndex,
		"moduleCategoryId": _topClassValue,
		"name":_searchKey
	}
	var url = config_gl.apiPath+"module/listByCategoryId";
	$.ajax(url, {
		type: 'post', //HTTP请求类型
		contentType: "application/json",
		data: JSON.stringify(fdata),
		success: function(data) {
			callBack(data);
		},
		error: function(xhr, type, errorThrown) {
			layer.alert("获取产品分类未知错误");
		}
	});
}

function renderImgList(){
	getProductDatas(function(data){
		var uldom = panel.find(".productList");
		uldom.empty();
		panel.find(".curPage").text(_currPageIndex);
		var code = data.code;
		if(code == 0){
			var list = data.data.records;
			for(var i=0; i<list.length; i++){
				var d = list[i];
				var pid = d.id;
				_productMap[pid] = d;
				var pimg = d.views[0].texture;
				var li = '<div class="procuctListImg" data-productid="'+pid+'"><img class="proImg" src="'+pimg+'"></div>'
				uldom.append(li);
			}
			
			uldom.find(".procuctListImg").on("click", changeProcuct);
			
			_pageMax = Math.ceil(data.data.total / _limit);
			panel.find(".totalPage").text(_pageMax);
		}else{
			layer.msg(data.message);
		}
	});
}

function changeProcuct(){
	var id = $(this).data("productid");
	var info = _productMap[id];
	if(info){
		var obj = {};
		obj.id = info.id;
		obj.faces = info.views;
		obj.colors = info.colorInfo;
		obj.sizes = info.size;
		obj.designType = info.designType;
		Design.event.trigger(Design.event.ADD_PRODUCT, obj);
	}
}


panel.find(".prePage").on("click", doPrePage);
panel.find(".nextPage").on("click", doNextPage);
panel.find(".choosePageGo").on("click", pageGoHandler);

/* 上一页 */
function doPrePage(){
	if(_currPageIndex>1){
		_currPageIndex--;
		renderImgList();
	}
}

/* 下一页 */
function doNextPage(){
	if(_currPageIndex<_pageMax){
		_currPageIndex++;
		renderImgList();
	}
}

/* 页面跳转 */
function pageGoHandler(){
	var goIndex = panel.find(".choosePage").val();
	goIndex = parseInt(goIndex);
	if(goIndex>0 && goIndex<=_pageMax){
		if(!isNaN(goIndex) && goIndex != _currPageIndex){
			_currPageIndex = goIndex;
			renderImgList();
		}
	}else{
		panel.find(".choosePage").text("");
	}
}

module.exports = exports;